/*
 * @Author: LIFM0623 1944099171@qq.com
 * @Date: 2024-01-12 21:45:09
 * @LastEditors: LIFM0623 1944099171@qq.com
 * @LastEditTime: 2024-03-29 20:43:09
 * @FilePath: \Scene_Back\src\marker\marker.service.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Injectable } from '@nestjs/common';
import { updateMarker } from './dto/update-marker.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { marker } from './entities/marker.entity';
import { Repository, In } from 'typeorm';
import { CreateMarkerDto } from './dto/create-marker.dto';

@Injectable()
export class MarkerService {
  constructor(
    @InjectRepository(marker)
    private readonly markerRepository: Repository<marker>,
  ) {}

  async createMarkerItem(rawMarkerDto: CreateMarkerDto) {
    const marker = this.markerRepository.create(rawMarkerDto);
    const insertMarker = await this.markerRepository.insert(marker);
    return insertMarker.raw;
  }

  async createMarkerItemForScript(rawMarkerDto: CreateMarkerDto) {
    const marker = this.markerRepository.create(rawMarkerDto);
    const insertMarker = await this.markerRepository.insert(marker);
    return insertMarker.raw[0].id;
  }

  async findByIds(ids: string[]): Promise<marker[]> {
    return this.markerRepository.find({
      where: {
        id: In(ids), // 使用TypeORM的In操作符
      },
    });
  }

  async findAll() {
    return this.markerRepository.find();
  }

  async findOne(id: string): Promise<marker> {
    return this.markerRepository.findOne({ where: { id: id } });
  }

  async getMarkerItemById(id: string): Promise<marker> {
    return this.markerRepository.findOne({ where: { id: id } });
  }
  async updateMarkerItemById(id: string, updateMarkerItem: updateMarker) {
    const oldMarker = await this.markerRepository.findOne({
      where: { id: id },
    });
    if (!oldMarker) {
      return '图片不存在';
    }

    const updateMarker = Object.assign(oldMarker, updateMarkerItem);
    await this.markerRepository.save(updateMarker);
    return '修改图片属性成功！';
  }
  async remove(id: string) {
    const list = await this.markerRepository.delete({ id: id });
    if (list) {
      return '删除成功！';
    } else {
      return '删除失败！';
    }
  }
}
